package com.lm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lm.dao.TDmMajorMapper;
import com.lm.dao.TEntityMajorMapper;
import com.lm.dao.TEntityUniversityMapper;
import com.lm.model.*;
import com.lm.model.TEntityMajorExample;
import com.lm.service.MajorService;
import com.lm.util.*;
import com.lm.vo.UniversitySimple;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName MajorServiceImpl
 * @Description TODO
 * @Author wn
 * @Date 2019/3/31 16:18
 * @Version 1.0
 **/
@Service
@Transactional
public class MajorServiceImpl implements MajorService {
    @Autowired
    private TEntityMajorMapper majorMapper;
    @Autowired
    private TDmMajorMapper dmMajorMapper;
    @Autowired
    private TEntityUniversityMapper universityMapper;

    @Override
    public Integer addEntity(TEntityMajor major) {
        String currentUserId = SecurityUitil.getCurrentUserId();
        major.setCreateby(currentUserId);
        major.setEditby(currentUserId);
        major.setId(Guid.get());
        major.setCreatetime(new Date());
        major.setEdittime(new Date());
        major.setStatue("1");
        return majorMapper.insert(major);
    }

    @Override
    public Integer updateEntity(TEntityMajor major) {
        TEntityMajor TEntityMajor = majorMapper.selectByPrimaryKey(major.getId());
        major.setEditby(SecurityUitil.getCurrentUserId());
        major.setEdittime(new Date());
        major.setStatue(TEntityMajor.getStatue());
        major.setCreateby(TEntityMajor.getCreateby());
        major.setCreatetime(TEntityMajor.getCreatetime());
        return majorMapper.updateByPrimaryKey(major);
    }

    @Override
    public Integer updateStatue(String id) {
        TEntityMajor entityMajor = majorMapper.selectByPrimaryKey(id);
        entityMajor.setStatue("0");
        return majorMapper.updateByPrimaryKey(entityMajor);
    }

    @Override
    public PageInfo getList(String keyword, Integer pageSize, Integer pageNumber) {
        TEntityMajorExample example = new TEntityMajorExample();
        TEntityMajorExample.Criteria criteria = example.createCriteria().andStatueEqualTo("1");
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andNameLike("%"+keyword+"%");
        }
        PageHelper.startPage(pageSize,pageNumber);
        return new PageInfo(majorMapper.selectWithOtherByExample(example));
    }

    @Override
    public List<TEntityMajor> getListSimple() {
        TEntityMajorExample example = new TEntityMajorExample();
        example.createCriteria().andStatueEqualTo("1");
        return majorMapper.selectByExample(example);
    }

    @Override
    public ExcelBaseBean getTemplate() {
        ExcelBaseBean baseBean = new ExcelBaseBean();
        baseBean.setCategory("院校信息");
        baseBean.setManager("计算机系主任");
        baseBean.setCompany("山东科技大学");
        baseBean.setSubject("院校信息表");
        baseBean.setTitle("专业信息");
        baseBean.setAnthor("wn");
        baseBean.setCommnets("无");
        baseBean.setExcelName("专业信息" + Guid.get() + ".xls");
        List<ExcelSheet> excelSheets = new ArrayList<>();
        ExcelSheet sheet = new ExcelSheet();
        sheet.setCreateSheet("专业信息表");
        List<ExcelSheetTitle> sheetTitles = new ArrayList<>();
        ExcelSheetTitle title1 = new ExcelSheetTitle(0,10 * 256,"专业名称");
        ExcelSheetTitle title2 = new ExcelSheetTitle(1,10 * 256,"英文名称");
        ExcelSheetTitle title3 = new ExcelSheetTitle(2,10 * 256,"学校");
        ExcelSheetTitle title4 = new ExcelSheetTitle(3,10 * 256,"层次(专科,本科,硕士，博士)");
        ExcelSheetTitle title5 = new ExcelSheetTitle(4,10 * 256,"等级(A+ -> C-)");
        ExcelSheetTitle title6 = new ExcelSheetTitle(5,10 * 256,"专业办学类型(普通,校企合作,中外合作)");
        ExcelSheetTitle title7 = new ExcelSheetTitle(6,10 * 256,"学科门类(二级或三级)");
        sheetTitles.add(title1);
        sheetTitles.add(title2);
        sheetTitles.add(title3);
        sheetTitles.add(title4);
        sheetTitles.add(title5);
        sheetTitles.add(title6);
        sheetTitles.add(title7);
        sheet.setSheetTitles(sheetTitles);
        excelSheets.add(sheet);
        baseBean.setSheets(excelSheets);
        return baseBean;
    }

    @Override
    public List<TDmMajor> getAllDmMajor() {
        TDmMajorExample example = new TDmMajorExample();
        example.createCriteria();
        return dmMajorMapper.selectByExample(example);
    }

    @Override
    public List<UniversitySimple> getAllUniversity() {
        return universityMapper.selectSimpleAll();
    }

    @Override
    public int addEntities(List<TEntityMajor> majors) {
        int suc = 0;
        for (TEntityMajor major : majors) {
            String currentUserId = SecurityUitil.getCurrentUserId();
            major.setCreateby(currentUserId);
            major.setEditby(currentUserId);
            major.setId(Guid.get());
            major.setCreatetime(new Date());
            major.setEdittime(new Date());
            major.setStatue("1");
            suc += majorMapper.insert(major);
        }
        return suc;
    }
}
